<?php

namespace app\web\controller;
use app\web\model\BusiCardModel;
use app\web\model\TradeModel;
use app\common\controller\StoreBaseController;
use think\Db;

/**
 * 名片信息
 */
class BusiCardController extends StoreBaseController
{
 	protected $model;
	protected $trademodel;
	
    protected function _initialize() {
        parent::_initialize();
		$this->trademodel = new TradeModel();
        $this->model = new BusiCardModel();
		$trdist = $this->trademodel->select();
		$this->assign('trd_list',$trdist);
    }
	
    /**
     * 名片信息
     * @author [longx] 
     */
    public function index(){
    	$store_id = session('storeInfo')['id'];
    	$busicard = db('action_busicard')->where('store_id',$store_id)->find();
		if($busicard){
	    	if(request()->isAjax()){
	            $param = input('post.');  			       
	         	if ($this->model->allowField(true)->save($param,$param['id']) !== false) {
	                    $this->success('保存成功', url('busi_card/index'));
	            } else {
	                $this->error('保存失败');
	            }
	        }
		}else{
			if(request()->isAjax()){
	        	$param = input('post.');
				$param['store_id'] = $store_id;
				$param['name'] = session('storeInfo')['store_name'];
				if ($this->model->allowField(true)->save($param) !== false) {
	                    $this->success('保存成功', url('busi_card/index'));
	            } else {
	                $this->error('保存失败');
	            }
	        }
		}

		$shareUrl = "http://www.auth.cn/web/busi_card/index.html";
		$agent['shareUrl'] = 'http://www.vshop88.net/code/code.php?url='.urlencode($shareUrl).'&margin=1&size=10';
        $this->assign('ad',$busicard);
		$this->assign('code_url',$agent['shareUrl']);
		$this->assign('url',$shareUrl);
        return $this->fetch();
    }
}